package com.github.florent37.shapeofview.shapes;


import ohos.agp.components.AttrSet;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.github.florent37.shapeofview.ShapeOfView;
import com.github.florent37.shapeofview.manager.ClipPathManager;
import com.github.florent37.shapeofview.manager.ResourceTable;

/**
 * DiagonalView
 *
 * @since 2021-07-26
 */
public class DiagonalView extends ShapeOfView {
    public static final int POSITION_BOTTOM = 1;
    public static final int POSITION_TOP = 2;
    public static final int POSITION_LEFT = 3;
    public static final int POSITION_RIGHT = 4;

    public static final int DIRECTION_LEFT = 1;
    public static final int DIRECTION_RIGHT = 2;

    private int diagonalPosition = POSITION_BOTTOM;
    private float diagonalAngle = 0f;

    /**
     * DiagonalView
     *
     * @param context context
     */
    public DiagonalView(Context context) {
        super(context);
        init(context, null);
    }

    /**
     * DiagonalView
     *
     * @param context context
     * @param attrs attrs
     */
    public DiagonalView(Context context, AttrSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    /**
     * DiagonalView
     *
     * @param context context
     * @param attrs attrs
     * @param defStyleAttr defStyleAttr
     */
    public DiagonalView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttrSet attrs) {
        setDrawColor(Color.getIntColor("#AAAAAA"));
        if (attrs != null) {
            try {
                String[] tabsTitle = getResourceManager().getElement(ResourceTable.Strarray_shape_diagonal_position).getStringArray();
                diagonalAngle = attrs.getAttr("shape_diagonal_angle").get().getFloatValue();
                diagonalPosition = Integer.parseInt(tabsTitle[diagonalPosition - 1]);
            } catch (Exception e) {
            }
        }
        super.setClipPathCreator(new ClipPathManager.ClipPathCreator() {
            public Path createClipPath(int width, int height) {
                final Path path = new Path();
                final float diagonalAngleAbs = Math.abs(diagonalAngle);
                final boolean isDirectionLeft = getDiagonalDirection() == DIRECTION_LEFT;
                final float perpendicularHeight = (float) (width * Math.tan(Math.toRadians(diagonalAngleAbs)));
                switch (diagonalPosition) {
                    case POSITION_BOTTOM:
                        if (isDirectionLeft) {
                            path.moveTo(getPaddingLeft(), getPaddingRight());
                            path.lineTo(width - getPaddingRight(), getPaddingTop());
                            path.lineTo(width - getPaddingRight(), height - perpendicularHeight - getPaddingBottom());
                            path.lineTo(getPaddingLeft(), height - getPaddingBottom());
                            path.close();
                        } else {
                            path.moveTo(width - getPaddingRight(), height - getPaddingBottom());
                            path.lineTo(getPaddingLeft(), height - perpendicularHeight - getPaddingBottom());
                            path.lineTo(getPaddingLeft(), getPaddingTop());
                            path.lineTo(width - getPaddingRight(), getPaddingTop());
                            path.close();
                        }
                        break;
                    case POSITION_TOP:
                        if (isDirectionLeft) {
                            path.moveTo(width - getPaddingRight(), height - getPaddingBottom());
                            path.lineTo(width - getPaddingRight(), getPaddingTop() + perpendicularHeight);
                            path.lineTo(getPaddingLeft(), getPaddingTop());
                            path.lineTo(getPaddingLeft(), height - getPaddingBottom());
                            path.close();
                        } else {
                            path.moveTo(width - getPaddingRight(), height - getPaddingBottom());
                            path.lineTo(width - getPaddingRight(), getPaddingTop());
                            path.lineTo(getPaddingLeft(), getPaddingTop() + perpendicularHeight);
                            path.lineTo(getPaddingLeft(), height - getPaddingBottom());
                            path.close();
                        }
                        break;
                    case POSITION_RIGHT:
                        if (isDirectionLeft) {
                            path.moveTo(getPaddingLeft(), getPaddingTop());
                            path.lineTo(width - getPaddingRight(), getPaddingTop());
                            path.lineTo(width - getPaddingRight() - perpendicularHeight, height - getPaddingBottom());
                            path.lineTo(getPaddingLeft(), height - getPaddingBottom());
                            path.close();
                        } else {
                            path.moveTo(getPaddingLeft(), getPaddingTop());
                            path.lineTo(width - getPaddingRight() - perpendicularHeight, getPaddingTop());
                            path.lineTo(width - getPaddingRight(), height - getPaddingBottom());
                            path.lineTo(getPaddingLeft(), height - getPaddingBottom());
                            path.close();
                        }
                        break;
                    case POSITION_LEFT:
                        if (isDirectionLeft) {
                            path.moveTo(getPaddingLeft() + perpendicularHeight, getPaddingTop());
                            path.lineTo(width - getPaddingRight(), getPaddingTop());
                            path.lineTo(width - getPaddingRight(), height - getPaddingBottom());
                            path.lineTo(getPaddingLeft(), height - getPaddingBottom());
                            path.close();
                        } else {
                            path.moveTo(getPaddingLeft(), getPaddingTop());
                            path.lineTo(width - getPaddingRight(), getPaddingTop());
                            path.lineTo(width - getPaddingRight(), height - getPaddingBottom());
                            path.lineTo(getPaddingLeft() + perpendicularHeight, height - getPaddingBottom());
                            path.close();
                        }
                        break;
                }
                return path;
            }

            @Override
            public boolean requiresBitmap() {
                return false;
            }
        });
    }

    /**
     * setDiagonalPosition
     *
     * @param diagonalPosition diagonalPosition
     */
    public void setDiagonalPosition(int diagonalPosition) {
        this.diagonalPosition = diagonalPosition;
        requiresShapeUpdate();
    }

    /**
     * getDiagonalPosition
     *
     * @return 1
     */
    public int getDiagonalPosition() {
        return diagonalPosition;
    }

    /**
     * getDiagonalDirection
     *
     * @return 1
     */
    public int getDiagonalDirection() {
        return diagonalAngle > 0 ? DIRECTION_LEFT : DIRECTION_RIGHT;
    }

    /**
     * getDiagonalAngle
     *
     * @return 1L
     */
    public float getDiagonalAngle() {
        return diagonalAngle;
    }

    /**
     * setDiagonalAngle
     *
     * @param diagonalAngle diagonalAngle
     */
    public void setDiagonalAngle(float diagonalAngle) {
        this.diagonalAngle = diagonalAngle;
        requiresShapeUpdate();
    }
}
